package 变量替换;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Fang Ruichuan
 * @date 2023-01-13 13:00
 */
public class Demo {
    public static void main(String[] args) {
        int a = 100, b = 200;
        exchange1(a, b);
        exchange2(a, b);
        exchange3(a, b);
        exchange4(a, b);
        exchange5(a, b);
    }

    // 1、加减法
    public static void exchange1(int a, int b) {
        a = a + b;
        b = a - b;
        a = a - b;
        System.out.println("加减法替换后 a = " + a + "， b = " + b);
    }

    // 2、乘除法
    public static void exchange2(int a, int b) {
        a = a * b;
        b = a / b;
        a = a / b;
        System.out.println("乘除法替换后 a = " + a + ", b = " + b);
    }

    // 3、异或法
    public static void exchange3(int a, int b) {
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println("异或法替换后 a = " + a + ", b = " + b);
    }

    // 4、使用数组
    public static void exchange4(int a, int b) {
        int[] arr = new int[]{a, b};
        System.out.println("使用数组后 a = " + arr[1] + ", b = " + arr[0]);
    }

    // 5、使用map
    public static void exchange5(int a, int b) {
        Map<String, Integer> map = new HashMap<>();
        map.put("a", a);
        map.put("b", b);
        a = map.get("b");
        b = map.get("a");
        System.out.println("使用map后 a = " + a + ", b = " + b);
    }
}
